<?php
class DBMAttribute {
	/**
	 * The name of this attribute
	 *
	 * @var string
	 */
	private $name;
	
	/**
	 * The column that represents this attribute
	 *
	 * @var string
	 */
	private $column;
	
	/**
	 * The value of the field
	 *
	 * @var mixed
	 */
	private $value = null;
	
	/**
	 * The alias of the field
	 *
	 * @var string
	 */
	private $alias = null;
	
	/**
	 * The father entity of this attribute
	 *
	 * @var DBMEntity
	 */
	private $entity = null;
	
	/**
	 * The type of the field
	 *
	 * @var string
	 */
	private $type;
	
	/**
	 * The size of the view
	 *
	 * @var int
	 */
	private $size = null;

	/**
	 * Define if this field is unique
	 *
	 * @var boolean
	 */
	private $unique = false;
	
	/**
	 * Define if this attribute is the primary key of part of it
	 *
	 * @var boolean
	 */
	private $primaryKey = false;
	
	/**
	 * Define if this field will be visible on data retrieve queries
	 *
	 * @var boolean
	 */
	private $visibleOnQuery = true;
	
	/**
	 * Define if this attribute is available for INSERT operations
	 *
	 * @var boolean
	 */
	private $insertable = true;
	
	/**
	 * Define if this attribute is available for UPDATE operations
	 *
	 * @var boolean
	 */
	private $updateable = true;
	
	/**
	 * Define if this attribute is available for SELECT operations
	 *
	 * @var boolean
	 */
	private $selectable = true;
	
	/**
	 * Define if this attribute is available for DELETE operations
	 *
	 * @var boolean
	 */
	private $deletable = true;
	
	/**
	 * Define if the field is read-only
	 *
	 * @var boolean
	 */
	private $readOnly = true;
	
	
	/** gets / sets */
	public function getName(){
		return $this->name;
	}
	public function setName($name){
		$this->name = $name;
	}
	public function getColumn(){
		return $this->column;
	}
	public function setColumn($column){
		$this->column = $column;
	}
	public function getValue(){
		return $this->value;
	}
	public function setValue($value){
		$this->value = $value;
	}
	public function getAlias(){
		if($this->alias == null){
			return $this->getColumn();
		} else {
			return $this->alias;
		}
	}
	public function setAlias($alias){
		$this->alias = $alias;
	}
	public function getEntity(){
		return $this->entity;
	}
	public function setEntity($entity){
		$this->entity = $entity;
	}
	public function getType(){
		return $this->type;
	}
	public function setType($type){
		$this->type = $type;
	}
	public function getSize(){
		return $this->size;
	}
	public function setSize($size){
		$this->size = $size;
	}
	public function isPrimaryKey(){
		return $this->primaryKey;
	}
	public function setPrimaryKey($primaryKey){
		$this->primaryKey = $primaryKey;
	}
	public function isUnique(){
		return $this->unique;
	}
	public function setUnique($unique){
		$this->unique = $unique;
	}
	public function isVisibleOnQuery(){
		return $this->visibleOnQuery;
	}
	public function setVisibleOnQuery($visibleOnQuery){
		$this->visibleOnQuery = $visibleOnQuery;
	}
	public function isInsertable(){
		return $this->insertable;
	}
	public function setInsertable($insertable){
		$this->insertable = $insertable;
	}
	public function isUpdateable(){
		return $this->updateable;
	}
	public function setUpdateable($updateable){
		$this->updateable = $updateable;
	}
	public function isSelectable(){
		return $this->selectable;
	}
	public function setSelectable($selectable){
		$this->selectable= $selectable;;
	}
	public function isDeletable(){
		return $this->deletable;
	}
	public function setDeletable($deletable){
		$this->deletable = $deletable;
	}
	public function isReadOnly(){
		return $this->readOnly;
	}
	public function setReadOnly($readOnly){
		$this->readOnly = $readOnly;
	}
	
	
	/** business */
	public function __construct($name, $column, $alias = null, &$entity = null, $options = array()){
		// set the name
		$this->setName($name);
		
		// set the column
		$this->setColumn($column);
		
		// set the alias
		$this->setAlias($alias);
		
		// set the table
		$this->setEntity($entity);
		
		// parse the options
		$this->parseOptions($options); 
	}
	
	private function parseOptions($options){
		if(isset($options["alias"]) && is_string($options["alias"])){
			$this->setAlias($options["alias"]);
		}
		if(isset($options["visible_on_query"]) && is_bool($options["visible_on_query"])){
			$this->setVisibleOnQuery($options["visible_on_query"]);
		}
	}

	public function __toString(){
		return $this->getEntity()->getAlias() . "__" . $this->getAlias();
	}
}

?>